import Vue from 'vue'
import VueRouter from 'vue-router'

// 登录页面
import Login from '../views/Login.vue'
// 主页
import Home from '../views/Home.vue'
// 欢迎
import Welcome from '../views/Welcome.vue'
// 用户列表组件
import User from '../views/user/Users.vue'

//权限列表组件
import Rights from '../views/power/Rights.vue'
//角色列表区域
import Roles from '../views/power/Roles.vue'

//商品分类模块
import Cate from '../views/goods/Cate.vue'
//分类参数组件
import Params from '../views/goods/Params.vue'
//商品列表模块
import List from '../views/goods/List.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component:Welcome
      },
      {
        path: '/users',
        component:User
      },
      {
        path: '/rights',
        component:Rights
      },
      {
        path: '/roles',
        component:Roles
      },
      {
        path: '/categories',
        component:Cate
      },
      {
        path: '/params',
        component:Params
      },
      {
        path: '/goods',
        component:List
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 使用导航守卫对页面权限管理，在没有token的情况下返回到登录页面
router.beforeEach((to, from, next) => {
  // 路径是 /login 就不验证token了
  if (to.path === '/login') return next()
  // 没有找到token 强制返回到 /login 页面
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
